﻿using App.IRepository;
using App.Model;
using App.Model.DTO.Article;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace App.Repository
{
    public class BaseRepository<TEntity> : SimpleClient<TEntity>, IBaseRepository<TEntity> where TEntity : class, new()
    {
        public BaseRepository(ISqlSugarClient context) : base(context)
        {
            base.Context = context;
            ////创建数据库
            //base.Context.DbMaintenance.CreateDatabase();
            ////创建表
            //base.Context.CodeFirst.InitTables(
            //    typeof(Article),
            //    typeof(Classification),
            //    typeof(UserInfoRepository)
            //    );
        }

        public async Task<bool> CreateAsync(TEntity entity)
        {
            return await base.InsertAsync(entity);
        }

        public async Task<bool> DeleteAsync(string id)
        {
            return await base.DeleteByIdAsync(id);
        }


        public async Task<bool> EditAsync(TEntity entity)
        {
            return await base.UpdateAsync(entity);
        }
        //导航查询
        public virtual async Task<TEntity> FindAsync(string id)
        {
            return await base.GetByIdAsync(id);
        }

        public async Task<TEntity> FindAsync(Expression<Func<TEntity, bool>> func)
        {
            return await base.GetSingleAsync(func);
        }

        public virtual async Task<List<TEntity>> QueryAsync()
        {
            return await base.GetListAsync();
        }

        public virtual async Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> func)
        {
            return await base.GetListAsync(func);
        }

        public virtual async Task<List<TEntity>> QueryAsync(int pageIndex, int pageSize, RefAsync<int> total)
        {
            return await base.Context.Queryable<TEntity>()
                .ToPageListAsync(pageIndex, pageSize, total);
        }

        public virtual async Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> func, int pageIndex, int pageSize, RefAsync<int> total)
        {
            return await base.Context.Queryable<TEntity>()
                .Where(func)
                .ToPageListAsync(pageIndex, pageSize, total);
        }

        public async Task<bool> BatchDeleteAsync(List<string> ids)
        {
            return await base.Context.Deleteable<TEntity>().In(ids).ExecuteCommandHasChangeAsync();
        }


        public async Task<bool> BatchPublishAsync(List<string> ids)
        {
            return await base.Context.Updateable<Article>()
                .SetColumns(it => new Article() { Status = ArticleStatusEnum.PUBLISH })
                .Where(it => ids.Contains(it.Id))
                .ExecuteCommandHasChangeAsync();
        }

        public async Task<bool> BatchUnpublishAsync(List<string> ids)
        {
            return await base.Context.Updateable<Article>()
                .SetColumns(it => new Article() { Status = ArticleStatusEnum.DRAFT })
                .Where(it => ids.Contains(it.Id))
                .ExecuteCommandHasChangeAsync();
        }

        public async Task<bool> BatchRestoreAsync(List<string> ids)
        {
            return await base.Context.Updateable<Article>()
                .SetColumns(it => new Article { IsDelete = false })
                .Where(it => ids.Contains(it.Id))
                .ExecuteCommandHasChangeAsync();
        }

        public async Task<List<Article>> QuerySoftDeletedAsync(Expression<Func<Article, bool>> func)
        {
            return await base.Context.Queryable<Article>()
                .Where(func)
                .Where(a => a.IsDelete)  // 只查询已软删除的文章
                .ToListAsync();
        }

        public async Task<List<Article>> QuerySoftDeletedAsync(int pageIndex, int pageSize, RefAsync<int> total)
        {
            return await base.Context.Queryable<Article>()
                .Where(a => a.IsDelete)  // 只查询已软删除的文章
                .ToPageListAsync(pageIndex, pageSize, total);
        }

        public virtual async Task<List<Article>> QuerySoftDeletedAsync(Expression<Func<Article, bool>> func, int pageIndex, int pageSize, RefAsync<int> total)
        {
            return await base.Context.Queryable<Article>()
                .Where(a => a.IsDelete)
                .Where(func)
                .ToPageListAsync(pageIndex, pageSize, total);
        }

        public async Task<bool> BatchSoftDeleteAsync(List<string> ids)
        {
            return await base.Context.Updateable<Article>()
                .SetColumns(it => new Article { IsDelete = true })
                .Where(it => ids.Contains(it.Id))
                .ExecuteCommandHasChangeAsync();
        }
    }
}
